// pages/car/index.js
// 购物车页面js
const app = getApp()
Page({
    data: {
        itemList: [],
        isAll: false,
        url: app.globalData.url,
        total: 0,
        checkList: []
    },
    // 点击选择框
    check(e) {
        let total = 0
        const itemList = this.data.itemList
        let checkList = []
        e.detail.value.forEach(i => {
            total += itemList[i].goods.killPrice
            checkList.push(itemList[i])
        })
        this.setData({ total: total.toFixed(2), checkList })
        if (e.detail.value.length === this.data.itemList.length) {
            this.setData({ isAll: true })
        } else {
            this.setData({ isAll: false })
        }
    },
    // 全选按钮
    checkAll(e) {
        if (e.detail.value.indexOf("total") != -1) { // 点击全选按钮
            let itemList = this.data.itemList;
            let total = 0
            let checkList = []
            itemList.forEach(i => {
                i.checked = true;
                total += i.goods.killPrice
                checkList.push(i)
            })
            this.setData({ itemList, total: total.toFixed(2), checkList })

        } else {
            let itemList = this.data.itemList;
            itemList.forEach(i => {
                i.checked = false;
            })
            this.setData({ itemList, total: 0 })
        }
    },
    // 页面加载生命周期
    onShow() {
        // 获取登录用户信息
        const user = wx.getStorageSync("user")
        if (user) {
            this.setData({ user })
        }
        const _this = this
        wx.request({
            // 查询在购物车的清单
            url: this.data.url + "/car/list",
            method: "GET",
            data: { uid: _this.data.user.id },
            success(res) {
                if (res.data.code === 1) {
                    _this.initData(res.data.result)
                } else {
                    wx.showModal({
                        title: res.data.msg
                    })
                }
            }
        })
    },
    to_login() {
        const appid = "wx8efd630c320af804"
        const secret = "1174ac6e704e3ca93e12eb5b5eeec336"
        const that = this
        wx.login({
            success(res) {
                if (res.code) {
                    // 将登录凭证发送到服务端
                    wx.request({
                        url: 'https://api.weixin.qq.com/sns/jscode2session?appid='
                            + appid + '&secret=' + secret + '&js_code=' + res.code
                            + '&grant_type=authorization_code',
                        method: 'GET',
                        success(res) {
                            wx.request({
                                url: that.data.url + "/user/login",
                                data: {
                                    openid: res.data.openid
                                },
                                success(res) {
                                    if (res.data.code == 1) {
                                        console.log(res.data.result);
                                        wx.setStorageSync("user", res.data.result)
                                        that.setData({ user: res.data.result })
                                        wx.showToast({ title: '登录成功' })
                                        const pages = getCurrentPages()
                                        const perpage = pages[pages.length - 1]
                                        perpage.onLoad()
                                    }
                                }
                            })
                        }
                    })
                }
            }
        })
    },
    // 解析结果集 将订单中还未支付的订单放到列表中
    initData(data) {
        let itemList = []
        data.forEach(e => {
            itemList.push(e);
        })
        this.setData({ itemList })
    },
    // 结算
    settlement() {
        const _this = this
        if (this.data.total == 0) { // 未选中商品
            wx.showToast({
                title: '请选择一件商品',
                icon: 'error'
            })
        } else {
            console.log(_this.data.checkList);
            let list = []
            // 先把car对象转换成order对象再跳转
            _this.data.checkList.forEach(car => {
                wx.request({
                    url: this.data.url + "/kill/carToPay",
                    data: car,
                    method: "POST",
                    success(res) {
                        if (res.data.code === 1) {
                            console.log(res.data.result);
                            list.push(res.data.result)
                            if (list.length == _this.data.checkList.length) {
                                wx.navigateTo({
                                    url: '/pages/order/index?list=' + JSON.stringify(list) + "&total=" + _this.data.total,
                                })
                            }
                        }
                    } 
                })
            })
        }
    },
    onHide() {
        const itemList = this.data.itemList
        itemList.forEach(item => {
            item.checked = false
        })
        this.setData({ isAll: false, itemList, total: 0 })
    }
})